package com.xwj.auction.dao;

import com.xwj.auction.domain.RobAuctionItemOrder;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * @author xwj
 * @date 2020/5/9
 */
@Repository
@Mapper
public interface RobAuctionItemOrderMapper {
    @Select("select * from rob_auction_item_order where item_id = #{itemId} and status=0 limit 1")
    @Results(id="selectByItemId", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "item_id", property = "itemId"),
            @Result(column = "status", property = "status"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "pay_dead_time", property = "payDeadTime")
    })
    RobAuctionItemOrder selectByItemId(Long itemId);

    @Update("update rob_auction_item_order set status=1 where id=#{id}")
    int cancelOrderById(Long id);

    @Insert("insert into rob_auction_item_order(user_id,item_id,status,create_time,pay_dead_time) " +
            "values(#{userId},#{itemId},#{status},#{createTime},#{payDeadTime})")
    int insert(RobAuctionItemOrder order);

    @Select("select count(*) from rob_auction_item_order where item_id = #{itemId} and user_id=#{userId}")
    int selectByItemIdAndUseId(@Param("itemId") Long itemId, @Param("userId") Integer userId);

    @Select("select * from rob_auction_item_order where item_id = #{itemId} order by id desc limit 1")
    @Results(id="selectLatestOrder", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "item_id", property = "itemId"),
            @Result(column = "status", property = "status"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "pay_dead_time", property = "payDeadTime"),
            @Result(column = "version", property = "version")
    })
    RobAuctionItemOrder selectLatestOrder(Long itemId);

    @Update("update rob_auction_item_order set status=#{updateStatus},version=version+1 where id=#{id} and version=#{expectVersion}")
    int cancelOrderByVersion(Long id, Integer expectVersion, int updateStatus);
}
